package com.boruan.qq.zxgylibrary.base;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.boruan.qq.zxgylibrary.constants.AllActivitiesHolder;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 崔琦 on 2018/7/22.
 * des: 所有Activity的父类
 */

public abstract class BaseActivity extends AppCompatActivity {

    private Context mContext;
    Unbinder unbinder;
    public WebView commonWeb;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AllActivitiesHolder.addAct(this);
        setContentView(getLayoutId());
        //ScreenAdapterTools.getInstance().loadView(getWindow().getDecorView());
        mContext = this;
        unbinder = ButterKnife.bind(this);
        /** 禁止横屏*/
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        /** 将状态栏字体颜色设置成黑色 */
        //getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        initWeb();
        init(savedInstanceState);
    }

    public void initWeb(){
        commonWeb = new WebView(mContext);
        commonWeb.getSettings().setJavaScriptEnabled(true);
        commonWeb.getSettings().setSupportZoom(true);
        commonWeb.getSettings().setBuiltInZoomControls(true);
        commonWeb.getSettings().setDisplayZoomControls(false);

        commonWeb.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        commonWeb.getSettings().setLoadWithOverviewMode(true);
        commonWeb.getSettings().setUseWideViewPort(true);
        commonWeb.getSettings().setTextZoom(100);

        commonWeb.setVerticalScrollBarEnabled(false);
        commonWeb.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);//默认先不使用缓存
        commonWeb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); //取消滚动条白边效果
        commonWeb.getSettings().setDefaultTextEncodingName("UTF-8");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            commonWeb.getSettings().setMixedContentMode(commonWeb.getSettings().MIXED_CONTENT_ALWAYS_ALLOW);  //注意安卓5.0以上的权限
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        AllActivitiesHolder.removeAct(this);
    }

    public Context getContext(){
        return mContext;
    }
    protected abstract int getLayoutId();
    protected abstract void init(Bundle savedInstanceState);

}
